package com.app.beans;

import com.app.model.Equipement;
import com.app.model.Interventions;
import com.app.model.Personnel;
import com.app.interfaces.interfaceService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.richfaces.component.UIDataTable;
/**
 *
 * @author Krachleur
 */
public class InterventionsBean extends MessageBean implements Serializable {
    private Interventions interv;
    private interfaceService intervService;
    private interfaceService persService;
    private interfaceService equipService;
    private List interv_list = new ArrayList();
    private List person_list = new ArrayList();
    private Hashtable perslist = new Hashtable();
    private List equip_list = new ArrayList();
    private Hashtable equiplist = new Hashtable();
    private String current_personnel;
    private String current_equipement;
    private boolean init;
    private Integer index;
    private String message;
    private boolean nouveau;
    private UIDataTable dataTable;

    public void viderChamps(){
        message = "";
        changerEtat();
        this.interv = new Interventions();
        nouveau = true;
    }

    public void annuler(){
        message = "";
        interv_list = (List)getInterv_list();
        interv = (Interventions)interv_list.get(index);
        changerEtat();
    }

    public void chargerCombo(){
        current_personnel = this.interv.getPersonnel().getPersNom() + "-" + this.interv.getPersonnel().getPersPrenom();
        current_equipement = this.interv.getEquipement().getEqLib();
    }

    /** Creates a new instance of InterventionsBean */
    public InterventionsBean() {
        this.interv = new Interventions();
        this.interv.setEquipement(new Equipement());
        this.interv.setPersonnel(new Personnel());
    }

    public void create(){
        if(nouveau){
            try{
                this.interv.setPersonnel((Personnel)perslist.get(current_personnel));
                this.interv.setEquipement((Equipement)equiplist.get(current_equipement));
                this.intervService.save(interv);
                message = mess_insert_true;
                style_message = "valid_message";
                chargerCombo();
            }catch(Exception e){
                e.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        } else {
            try{
                this.interv.setPersonnel((Personnel)perslist.get(current_personnel));
                this.interv.setEquipement((Equipement)equiplist.get(current_equipement));
                this.intervService.modify(interv);
                message = mess_modif_true;
                style_message = "valid_message";
                chargerCombo();
            }catch(Exception e){
                e.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        }
        changerEtat();
    }

    public void modifierLigne(){
        this.interv = (Interventions)dataTable.getRowData();
        chargerCombo();
        message = "";
        nouveau = false;
        changerEtat();
    }

    public void modifier(){
        message = "";
        nouveau = false;
        changerEtat();
    }

    public void supprimer(){
        try{
            this.intervService.delete(interv);
            message = mess_del_true;
            style_message = "valid_message";
            if(index > 0 ){
                index--;
            }
            interv_list = (List)getInterv_list();
            if(!interv_list.isEmpty()){
                interv = (Interventions)interv_list.get(index);
                chargerCombo();
            } else {
                viderChamps();
                changerEtat();
            }
        }catch(Exception e){
            e.printStackTrace();
            
        }
    }

    public String getCurrent_equipement() {
        return current_equipement;
    }

    public String getCurrent_personnel() {
        return current_personnel;
    }

    public UIDataTable getDataTable() {
        return dataTable;
    }

    public Hashtable getEquiplist() {
        equip_list.clear();
        equiplist.clear();
        List l = this.equipService.findAll();
        for(Iterator it = l.iterator(); it.hasNext();){
            Equipement equip = (Equipement)l.get(l.indexOf(it.next()));
            equip_list.add(equip.getEqLib());
            equiplist.put(equip.getEqLib(), equip);
        }
        return equiplist;
    }

    public interfaceService getEquipService() {
        return equipService;
    }

    public List getEquip_list() {
        return equip_list;
    }

    public Integer getIndex() {
        return index;
    }

    public boolean isInit() {
        message = "" ;
        getInterv_list();
        getEquiplist();
        getPerslist();
        if(!(interv_list.isEmpty())){
            interv_list = (List)getInterv_list();
            interv = (Interventions)interv_list.get(0);
            chargerCombo();
        }
        etat = true;
        invetat = !etat;
        return init;
    }

    public Interventions getInterv() {
        return interv;
    }

    public interfaceService getIntervService() {
        return intervService;
    }

    public List getInterv_list() {
        return interv_list;
    }

    public String getMessage() {
        return message;
    }

    public boolean isNouveau() {
        return nouveau;
    }

    public interfaceService getPersService() {
        return persService;
    }

    public Hashtable getPerslist() {
        person_list.clear();
        perslist.clear();
        List l = this.persService.findAll();
        for(Iterator it = l.iterator(); it.hasNext();){
            Personnel pers = (Personnel)l.get(l.indexOf(it.next()));
            person_list.add(pers.getPersNom() +"-"+pers.getPersPrenom());
            perslist.put(pers.getPersNom()+"-"+pers.getPersPrenom(), pers);
        }
        return perslist;
    }

    public List getPerson_list() {
        return person_list;
    }

    public void setCurrent_equipement(String current_equipement) {
        this.current_equipement = current_equipement;
    }

    public void setCurrent_personnel(String current_personnel) {
        this.current_personnel = current_personnel;
    }

    public void setDataTable(UIDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public void setEquiplist(Hashtable equiplist) {
        this.equiplist = equiplist;
    }

    public void setEquipService(interfaceService equipService) {
        this.equipService = equipService;
    }

    public void setEquip_list(List equip_list) {
        this.equip_list = equip_list;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public void setInit(boolean init) {
        this.init = init;
    }

    public void setInterv(Interventions interv) {
        this.interv = interv;
    }

    public void setIntervService(interfaceService intervService) {
        this.intervService = intervService;
    }

    public void setInterv_list(List interv_list) {
        this.interv_list = interv_list;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setNouveau(boolean nouveau) {
        this.nouveau = nouveau;
    }

    public void setPersService(interfaceService persService) {
        this.persService = persService;
    }

    public void setPerslist(Hashtable perslist) {
        this.perslist = perslist;
    }

    public void setPerson_list(List person_list) {
        this.person_list = person_list;
    }


}